package com.ck.company.controller.client;

import com.ck.annotation.PermissionRequired;
import com.ck.company.service.CpVideoService;
import com.ck.config.CustomProperties;
import com.ck.constant.FileConstant;
import com.ck.constant.PageConstant;
import com.ck.constant.RequestConstant;
import com.ck.constant.ResponseConstant;
import com.ck.model.common.User;
import com.ck.model.common.sys.JsonResponse;
import com.ck.model.company.vo.CpProgramVideoListVO;
import com.github.pagehelper.Page;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 视频
 *
 * @author linqiang
 * @date 2018-09-19 18:19
 */
@PermissionRequired
@Controller
@RequestMapping("/client/video")
public class CpVideoClientController {
    private static Logger logger = LoggerFactory.getLogger(CpVideoClientController.class);
    @Autowired
    private CpVideoService cpVideoService;
    @Autowired
    private CustomProperties customProperties;

    /**
     * 上传视频
     */
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Object upload(@RequestParam("files") MultipartFile files, HttpServletRequest request) {
        try {
            //视频全名（包含扩展名）
            String originalFilename = files.getOriginalFilename();
            if (StringUtils.isBlank(originalFilename)) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "视频名称不能为空");
            }
            if (files.getSize() > customProperties.getMaxVideoSize()) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "只能上传视频文件，且不超过"
                        + (customProperties.getMaxVideoSize() / 1024 / 1024) + "M");
            }
            //视频后缀
            String extension = FilenameUtils.getExtension(originalFilename);
            //验证后缀名
            if (!FileConstant.VIDEO_SUFFIX_LIST.contains(extension)) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "文件格式不正确，请上传视频文件");
            }
            //视频名称
            String videoName = originalFilename.replaceAll("." + extension, "");
            if (StringUtils.isBlank(videoName.trim())) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "视频名称不能为空");
            }
            Map<String, Object> msgMap = cpVideoService.saveVideo(files, request);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, msgMap, "上传视频成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpVideoClientController uploadVideo 上传视频失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "上传视频失败");
    }

    /**
     * 视频列表
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(String videoName, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        try {
            User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
            String companyCode = loginUser.getCompanyCode();
            pageNum = (pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum);
            pageSize = (pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize);
            Page<CpProgramVideoListVO> pageInfo = cpVideoService.listProgramVideo(companyCode, videoName, pageNum, pageSize);
            Map<String, Object> result = new HashMap<>(5);
            result.put("videoName", videoName);
            result.put("data", pageInfo.getResult());
            result.put("pageNum", pageNum);
            result.put("pageSize", pageSize);
            result.put("total", pageInfo.getTotal());
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, result, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpVideoClientController list 查询视频列表失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "查询失败");
    }
}
